package com.sgb.kjwl.view.ui.activity.self.authentication;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityOpenTicksBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.user.SupplierAuthEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.AuthEvent;
import com.sgb.kjwl.viewmodel.self.OpenTicksModel;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

public class OpenTicksInfoActivity extends AppActivity {
    private ActivityOpenTicksBinding binding;
    @Inject
    OpenTicksModel viewModel;

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, OpenTicksInfoActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(OpenTicksInfoActivity.this, R.layout.activity_open_ticks);
        binding.setViewModel(viewModel);
        binding.include.actionBarTitle.setText(getResources().getString(R.string.self_supplier_auth));
        binding.include.actionBarImageLeft.setVisibility(View.VISIBLE);
        binding.include.actionBarImageLeft.setOnClickListener(this);
        binding.nextView.setOnClickListener(this);
    }

    private void showInfoView(SupplierAuthEntity supplierAuthEntity) {
        viewModel.supplierAuthEntity.set(supplierAuthEntity);
        binding.itemTvName.setContent(supplierAuthEntity.getCompanyName());
        binding.itemTvCreditCode.setContent(supplierAuthEntity.getCreditCode());
        binding.itemTvRegisteredAddress.setContent(supplierAuthEntity.getCompanyAddress());
    }

    @Override
    protected void init() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.action_bar_image_left:
                finish();
                break;
            case R.id.next_view:
              //  CommitSuccessActivity.start(this);
                validation();
                break;
        }
    }

    private void validation() {
        viewModel.submitAuthInfoModel(viewModel.supplierAuthEntity.get());
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(AuthEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_SELF_TICKS_INFO) {
            SupplierAuthEntity supplierAuthEntity = (SupplierAuthEntity) event.getMessageData();
            showInfoView(supplierAuthEntity);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
